<h1><? if(@$data['LeaveType']['id']): ?>Edit LeaveType<? else: ?>Create LeaveType<? endif ?></h1>

<?= $this->Form->create('LeaveType', array('action'=>'save')) ?>
    <?= $this->Form->hidden('id', array('value'=>@$data['LeaveType']['id'])) ?>

    <p><?= $this->Form->input('name', array(
        'value' => @$data['LeaveType']['name'],
        'label' => 'Name:',
        'class' => 'medium'
    )) ?></p>

    <fieldset>
        <legend>Rules:</legend>
        <?php
            $rulesSelected = Set::extract('{n}.rule', @$data['LeaveTypeRule']);
            $rulesSelected = $rulesSelected ? $rulesSelected : array();
        ?>
        
        <? foreach($rules as $k=>$rule): ?>
            <?php
                $ruleArgs = $rule['arguments'];
                $selected = in_array($rule['name'], $rulesSelected);
            ?>
            <p class="rule"><?= $this->Form->checkbox("LeaveTypeRule.{$k}.rule", array(
                'value'       => $rule['name'],
                'checked'     => $selected,
                'hiddenField' => false,
                'onclick'     => "var args=$(this).siblings('.arguments');if(this.checked){args.addClass('selected')}else{args.removeClass('selected')}"
            )) ?> <?= $this->Form->label("LeaveTypeRule.{$k}.rule", $rule['name']) ?>
            
            <? if (! empty($ruleArgs)): ?>
                <span class="arguments<? if ($selected): ?> selected<? endif?>">
                    <? foreach ($ruleArgs as $argName=>$type): ?>
                        <? if (is_array($type)): ?>
                            <?php
                                $options = array_combine($type, $type);
                                $argVal  = @$data['LeaveTypeRuleIndexed'][$rule['name']]['arguments'][$argName];
                            ?>
                            <span>
                                <?= $this->Form->label("LeaveTypeRule.{$k}.arguments.".$argName, $argName.":") ?>
                                <?= $this->Form->select("LeaveTypeRule.{$k}.arguments.".$argName, $options, array(
                                    'value' => $argVal,
                                    'empty' => false
                                )) ?>
                            </span>
                        <? else: ?>
                            <?php
                                $argVal = @$data['LeaveTypeRuleIndexed'][$rule['name']]['arguments'][$argName];
                            ?>
                            <span>
                                <?= $this->App->ruleArgument("LeaveTypeRule.{$k}.arguments.".$argName, $type, array(
                                	'value' => $argVal,
                                    'label' => $argName.":"
                                )) ?>
                            </span>
                        <? endif ?>
                    <? endforeach ?>
                    </span>
            <? endif ?>
            </p>
        <? endforeach ?>
    </fieldset>
    
    <p><?= $this->Form->submit('Save', array('div'=>false)) ?>
    <?= $this->Html->link('Cancel', array('action'=>'index')) ?></p>
<?= $this->Form->end() ?>
